package vn.demand.dragon.model
{
	import mx.collections.XMLListCollection;
	import mx.controls.Alert;
	
	import org.puremvc.as3.interfaces.IProxy;
	import org.puremvc.as3.patterns.proxy.Proxy;
	
	import vn.demand.dragon.CommonCommand;
	import vn.demand.dragon.FFIAApplicationFacade;
	import vn.demand.dragon.model.business.ArchiveProjectDelegate;
	import vn.demand.dragon.model.business.CompleteProjectDelegate;
	import vn.demand.dragon.model.business.LoadReferenceDetailsDelegate;
	import vn.demand.dragon.model.business.LoadReferencesDelegate;
	import vn.demand.dragon.model.business.RemoveReferenceDelegate;
	import vn.demand.dragon.model.business.SaveProjectDelegate;
	
	public class ReferenceProxy extends Proxy implements IProxy
	{
		
		public static const NAME:String = "ReferenceProxy";
		
		public var errorStatus:String; 
		
		public function ReferenceProxy(proxyName:String=NAME, data:Object = null) 
        {
            super (proxyName, data );
        }
        
        public function loadReferences(params:Object = null):void {
			var delegate:LoadReferencesDelegate = new LoadReferencesDelegate(
				function(rpcEvent:Object):void {
					data = new XMLListCollection(rpcEvent.result.projects.item);
					RIALogger.debug(this, "load xong du lieu");
					sendNotification(FFIAApplicationFacade.LOAD_REFERENCE_SUCCESS);
				}, 
				function(rpcEvent:Object):void {
					errorStatus = "Could not load references!";
					sendNotification(FFIAApplicationFacade.LOAD_REFERENCE_FAILED, errorStatus);									
				});
			delegate.loadReferences(params);
		}
		
		public function getProjectById(id:int, property:String = ''):Object {
			RIALogger.debug(this, "======Project Id:" +id);
			var result:Object = null;
			for each(var x in projectsListDP) {
				RIALogger.debug(this, id + " >< " + Number(x['project_id']));
				if (Number(x['project_id']) == id) {
					RIALogger.debug(this, " >yeah< ");
					result = x;
					break;
				}
			}
        	if (result != null) {
        		if (result.hasOwnProperty(property)) {
        			return String(result[property]);
        		}
        		return result;
        	}
        	return null;
		}
		
		public function remove(params:Object):void {
			var delegate:RemoveReferenceDelegate = new RemoveReferenceDelegate(
				function(rpcEvent : Object) {
					Alert.show(rpcEvent.result);
					var success:Boolean = rpcEvent.result.success == 'true';
					if (success) {
						sendNotification(CommonCommand.DELETE_REFERENCE_SUCESSFUL, rpcEvent.result.project_iso_code);
					} else {
						sendNotification(CommonCommand.DELETE_REFERENCE_FAILED, String(rpcEvent.result.errors));
					}
				},
				function(rpcEvent : Object) {
					sendNotification(CommonCommand.DELETE_REFERENCE_FAILED, 'Some thing wrong...');
				});
			delegate.removeReference(params);
		}
		
		public function loadProject(project_id:int):void {
			var delegate:LoadReferenceDetailsDelegate = new LoadReferenceDetailsDelegate(
				function(rpcEvent:Object):void {
					sendNotification(FFIAApplicationFacade.LOAD_REFERENCE_DETAIL_SUCCESS, rpcEvent.result, rpcEvent.result.project_id); 
				},
				function(rpcEvent:Object):void {
					sendNotification(FFIAApplicationFacade.LOAD_REFERENCE_DETAIL_FAILED); 
				}
			);
			delegate.loadReference(project_id);
		}
		
		public function loadProjectByCode(projectCode:String, callerName:String = null):void {
			var noteType:String = callerName;
			var delegate:LoadReferenceDetailsDelegate = new LoadReferenceDetailsDelegate(
				function(rpcEvent:Object):void {
					var success = rpcEvent.result.success;
					if (success == false) {
						sendNotification(FFIAApplicationFacade.LOAD_REFERENCE_DETAIL_FAILED, rpcEvent.result, noteType); 
					} else {
						sendNotification(FFIAApplicationFacade.LOAD_REFERENCE_DETAIL_SUCCESS, rpcEvent.result, noteType); 
					}
				},
				function(rpcEvent:Object):void {
					sendNotification(FFIAApplicationFacade.LOAD_REFERENCE_DETAIL_FAILED, null, noteType); 
				}
			);
			delegate.loadReferenceByCode(projectCode);
		}
		
		public function get projectsListDP():XMLListCollection {
			return data as XMLListCollection;
		}
		
		public function save(project:Object):void {
			var delegate:SaveProjectDelegate = new SaveProjectDelegate(
				function(rcpEvent:Object):void {
					var success:Boolean = rcpEvent.result.success == 'true'; 
					if (success) {
						sendNotification(FFIAApplicationFacade.PROJECT_UPDATED, {'id': rcpEvent.result.project_id, 'iso_code': rcpEvent.result.project_iso_code}, project.project_iso_code);
					} else {
						sendNotification(FFIAApplicationFacade.PROJECT_UPDATE_FAILED, null, project.project_iso_code);				
					}
				}, 
				function(rpcEvent:Object):void {
					errorStatus = "Could not load references!";
					sendNotification(FFIAApplicationFacade.PROJECT_UPDATE_FAILED, errorStatus, project.project_code);									
				});
			delegate.saveProject(project);
		}
		
		public function archivedProjects(arrId:String):void {
			var delegate:ArchiveProjectDelegate = new ArchiveProjectDelegate(
				function(rcpEvent:Object):void {
					sendNotification(CommonCommand.PROJECTS_ARCHIVED, new XMLListCollection(rcpEvent.result.log));
				}, 
				function(rpcEvent:Object):void {
					sendNotification(CommonCommand.PROJECTS_ARCHIVE_FAILED, 'Something errors...');
				});
			delegate.archiveProject(arrId);
		}
		
		public function complete(projectVO:Object, completeDate:String='', on_step:String='VALIDATE'):void {
			var project_id:int = int(projectVO.project_id); 
			var delegate:CompleteProjectDelegate = new CompleteProjectDelegate(
				function(rcpEvent:Object):void {
					var success:Boolean = rcpEvent.result.success == 'true'; 
					var action:String = String(rcpEvent.token.action);
					RIALogger.debug(this, 'complete project, action = ' + action);
					if (success) {
						if (action == 'VALIDATE') {
							sendNotification(FFIAApplicationFacade.PROJECT_CAN_COMPLETE, {'id': project_id, 'messages': rcpEvent.result.messages});
						} else if (action == 'DO_COMPLETE'){
							sendNotification(FFIAApplicationFacade.PROJECT_COMPLETED, {'id': project_id, 'messages': rcpEvent.result.messages});
						}
					} else {
						if (action == 'VALIDATE') {
							sendNotification(FFIAApplicationFacade.PROJECT_CAN_NOT_COMPLETE, {'id': project_id, 'messages': rcpEvent.result.messages});
						} else if (action == 'DO_COMPLETE'){
							sendNotification(FFIAApplicationFacade.PROJECT_COMPLETE_FAILED, {'id': project_id, 'messages': rcpEvent.result.messages});
						}
					}
				}, 
				function(rcpEvent:Object):void {
					var action:String = String(rcpEvent.token.action);
					if (action == 'VALIDATE') {
						sendNotification(FFIAApplicationFacade.PROJECT_CAN_NOT_COMPLETE, {'id': project_id, 'messages': 'Unknow error'});
					} else if (action == 'DO_COMPLETE'){
						sendNotification(FFIAApplicationFacade.PROJECT_COMPLETE_FAILED, {'id': project_id, 'messages': 'Unknow error. May be error from server'});
					}
				});
				
			delegate.complete(project_id, completeDate, on_step);
		}
	
	}
}